package com.fingard.dsp.bank.directbank.bony01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.reqrespobj.RetAtsBase;
import com.fingard.net.WebRequest;
import com.fingard.xml.XmlTextWriter;


public class BONY01Base extends DirectBase {

    protected String transCode = "";//交易代码
    protected String versionId = "1.0";//版本号
    protected String seqNo = "";//交易流水号，查询类交易可以为空
    protected String busiDate = "";//业务日期，查询类交易可以为空
    protected String queryStartNo = "";//查询起始序号，翻页查询必输
    protected String queryNum = "";//每页查询笔数，翻页查询必输

    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};
        String httpUrl = bankFront.serverURL;
        String mac = new Mac().MAC(bankFront.keyID,"0000000000000000",p_reqStr,1);
        String reqStr = p_reqStr + mac;

        WriteBankLogLn("1.提交银行的地址：" + httpUrl);
        WriteBankLogStep2(reqStr);
        //发送给银行前置机
        WebRequest tmpWebRequest = new WebRequest(httpUrl);
        tmpWebRequest.setConnectTimeout(60000);
        byte[] tmpUpBytes = reqStr.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpUpBytes, getCharset());//发送报文+MAC校验码
        WriteBankLogStep3(retStr[1]);

        return retStr;
    }


    //socket
    /*protected String[] sendToBank1(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};
        WriteBankLogLn("1.提交银行的地址：" + bankFront.serverIP + ":" + bankFront.serverPort);
        //发送银行前置
        TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.serverPort), this);
        p_reqStr += p_reqStr + bankFront.keyID;
        byte[] tmpBytesBody = p_reqStr.getBytes(getCharset());

        WriteBankLogStep2(p_reqStr);
        TcpReceiver tmpReceiver = tmpClient.sendMessage(tmpBytesBody);
        retStr[0] = tmpReceiver.connState;
        if (tmpReceiver.recBytes != null) {
            retStr[1] = new String(tmpReceiver.recBytes, 0, tmpReceiver.readedLength, getCharset());
            WriteBankLogStep3(retStr[1]);
        } else {
            retStr[1] = tmpReceiver.getConnMsg();
        }
        if (!tmpReceiver.isCompleted) {
            WriteBankLogLn(tmpReceiver.getConnMsg());
        }
        return retStr;
    }*/

    /**
     * 写入通用头部节点
     *
     * @param xmlWriter 发银行报文的XML对象
     * @throws Exception
     */
    protected void writeCommonHeader(XmlTextWriter xmlWriter) throws Exception {
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        xmlWriter.writeStartElement("Root");
        xmlWriter.writeStartElement("Head");
        xmlWriter.writeElementString("TransCode", transCode);//交易代码
        Date dateNow = new Date();
        xmlWriter.writeElementString("TransDate", Format.Date8Format.format(dateNow));//交易日期
        xmlWriter.writeElementString("TransTime", Format.Time6Format.format(dateNow));//交易时间
        xmlWriter.writeElementString("OperaName", bankFront.operator);//操作人
        xmlWriter.writeElementString("EntrNo", bankFront.org);//企业代码
        xmlWriter.writeElementString("BusiDate", busiDate);//业务日期
        xmlWriter.writeElementString("SeqNo", seqNo);//交易流水号
        xmlWriter.writeElementString("VersionID", versionId);//版本号
        xmlWriter.writeElementString("QueryStartNo", queryStartNo);//查询起始序号
        xmlWriter.writeElementString("QueryNum", queryNum);//每页查询笔数
        xmlWriter.writeEndElement();

    }

    /**
     * 清除报文数据的空格和换行符
     *
     * @param xml
     * @return
     */
    protected static String clearLineWrapAndBlank(String xml) {
        return xml.replace("\r", "").replace("\n", "").replace(" ", "");
    }

    /**
     * 判断响应码
     *
     * @param retCode
     * @return
     */
    protected static boolean isActiveRetCode(String retCode) {
        if (retCode != null) {
            if (retCode.equals("00000")) {
                return true;
            }
        }
        return false;
    }

    /**
     * 银行返回响应码翻译
     *
     * @param retCode
     * @return
     * @throws Exception
     */
    protected void getRetInfo(RetAtsBase retAts, String retCode, String respMsg) throws Exception {
        retAts.writeRespCode("-1");
        if (retCode.equals("其他")) {
            retAts.writeRespInfo(respMsg);
        } else {
            retAts.writeRespInfo(getBankConfig().getMapValueByKey("respCodeMapping", retCode));
        }
    }

}
